package com.hemerasolutions.v2trainer.model.unit.volume;

import java.text.DecimalFormat;

import javax.persistence.Column;
import javax.persistence.Entity;
import com.hemerasolutions.v2trainer.model.base.BaseModel;

@Entity
public class VolumeFactor extends BaseModel {

	private static final long serialVersionUID = 1L;

    @Column(length = 30)
	private String name;
    
    @Column(length = 12)
	private String unit;

    @Column(length = 2)
	private int decimals;
	 
    @Column(length = 2)
    private int size;
    
    @Column
    private String downFormula;
    
    @Column
    private String upFormula;
    
    @Column
    private boolean timeBased = false;
    
    
	public VolumeFactor() {
		super();
	}


	public VolumeFactor(String name, String unit, int size, int decimals, 
			String upFormula, String downFormula, boolean timeBased) {
		super();
		this.name = name;
		this.unit = unit;
		this.decimals = decimals;
		this.size = size;
		this.downFormula = downFormula;
		this.upFormula = upFormula;
		this.timeBased = timeBased;
	}

	public String getDownFormula() {
		return downFormula;
	}

	public void setDownFormula(String downFormula) {
		this.downFormula = downFormula;
	}

	public String getUpFormula() {
		return upFormula;
	}

	public void setUpFormula(String upFormula) {
		this.upFormula = upFormula;
	}

	public String getPattern(){
		String pattern = "#0";
		
		
		if(decimals != 0){
			pattern = pattern.concat(".");
			for(int x = 0; x < decimals; x++ ){
				pattern = pattern.concat("#");
			}
		}
		
		return pattern;
	}

	public String format(double value){
		
		DecimalFormat df = new DecimalFormat(this.getPattern());
		
		String  textValue = df.format(value);

		return textValue;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public int getDecimals() {
		return decimals;
	}

	public void setDecimals(int decimals) {
		this.decimals = decimals;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public boolean isTimeBased() {
		return timeBased;
	}

	public void setTimeBased(boolean timeBased) {
		this.timeBased = timeBased;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		VolumeFactor other = (VolumeFactor) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}


	@Override
	public String toString() {
		return "VolumeFactor [id=" + id + ", name=" + name + "]";
	}

}
